package com.song.controller.gxtp;

import com.song.model.ResultEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.entity.gxtp.GxtpRyk;
import com.song.service.gxtp.GxtpRykService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
import java.util.Map;

/**
* <p>
    * 关系图谱_人员库 控制器
    * </p>
*
* @author song
* @since 2025-01-21
*/
@Api(tags = {"关系图谱_人员库 控制器"})
@RestController
@RequestMapping("/gxtp_ryk")
public class GxtpRykController {

    @Autowired
    GxtpRykService gxtpRykService;

    /**
    * 分页查询数据
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "按条件分页检索关系图谱_人员库",notes = "按条件分页检索关系图谱_人员库")
    @GetMapping(value = "/pageList")
    public ResultEntity<Page<GxtpRyk>> pageList(
        @Validated GxtpRyk gxtpRyk,
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,
        @ApiParam(value = "分页大小", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<GxtpRyk> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<GxtpRyk> lambda = new QueryWrapper<GxtpRyk>().lambda();
        if(ObjectUtils.isNotEmpty(gxtpRyk.getId())){
            lambda.eq(GxtpRyk::getId,gxtpRyk.getId());
        }
        if(ObjectUtils.isNotEmpty(gxtpRyk.getGmsfhm())){
            lambda.eq(GxtpRyk::getGmsfhm,gxtpRyk.getGmsfhm());
        }
        if(ObjectUtils.isNotEmpty(gxtpRyk.getXm())){
            lambda.eq(GxtpRyk::getXm,gxtpRyk.getXm());
        }
        return ResultEntity.success(gxtpRykService.page(page,lambda));
    }

    /**
    * 查询数据List
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "查询全部关系图谱_人员库",notes = "查询全部关系图谱_人员库")
    @GetMapping(value = "/list")
    public ResultEntity<List<GxtpRyk>> list() {
        List<GxtpRyk> gxtpRykList = gxtpRykService.list();
        return ResultEntity.success(gxtpRykList);
    }

    /**
    * 关系图谱_人员库新增
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "新增关系图谱_人员库",notes = "新增关系图谱_人员库")
    @PostMapping(value = "/add")
    public ResultEntity add(@RequestBody GxtpRyk gxtpRyk) {
        boolean exists = gxtpRykService.checkExists(gxtpRyk.getGmsfhm());
        if (exists){
            return ResultEntity.failMessage("公民身份号码已存在！");
        }
        gxtpRykService.save(gxtpRyk);
        return ResultEntity.success();
    }

    /**
    * 关系图谱_人员库修改
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "修改关系图谱_人员库",notes = "修改关系图谱_人员库")
    @PostMapping(value = "/update")
    public ResultEntity edit(@RequestBody GxtpRyk gxtpRyk) {
        boolean exists = gxtpRykService.checkExists(gxtpRyk.getGmsfhm());
        if (exists){
            return ResultEntity.failMessage("公民身份号码已存在！");
        }
        gxtpRykService.updateById(gxtpRyk);
        return ResultEntity.success();
    }

    /**
    * 删除关系图谱_人员库
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "按id删除关系图谱_人员库",notes = "按id删除关系图谱_人员库")
    @PostMapping(value = "/delete")
    public ResultEntity delete(@RequestParam Serializable id) {
        gxtpRykService.removeById(id);
        return ResultEntity.success();
    }


    /**
    * 根据id查询关系图谱_人员库
    *
    * @author song
    * @Date 2025-01-21
    */
    @ApiOperation(value = "根据id查询关系图谱_人员库",notes = "根据id查询修改关系图谱_人员库")
    @GetMapping(value = "/detail")
    public ResultEntity<GxtpRyk> detail(@RequestParam Serializable id) {
        GxtpRyk gxtpRyk = gxtpRykService.getById(id);
        return ResultEntity.success(gxtpRyk);
    }

    @ApiOperation(value = "身份证号获取人员关系图谱",notes = "身份证号获取人员关系图谱")
    @GetMapping(value = "/gxtp")
    public ResultEntity queryGxtp(@RequestParam String gmsfhm){
        Map<String, Object> resultMap = gxtpRykService.queryGxtp(gmsfhm);
        return ResultEntity.success(resultMap);
    }
}